package com.vrv.controller.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vrv.entity.DbInfo;
import com.vrv.entity.Form;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/local")
public class LocalSetController {

    /**
     * Description: <br>
     * 主机首页
     * 
     * @return <br>
     */
    @RequestMapping("/index")
    public String list() {
        return "/local/local_index";
    }

    /**
     * Description: <br>
     * 主机详情
     * 
     * @return <br>
     */
    @RequestMapping("/info")
    public String getLocalInfo(ModelMap model, String localId, String nodeIp) {
        model.put("localId", localId);
        model.put("nodeIp", nodeIp);
        return "/local/local_info";
    }

    /**
     * Description: <br>
     * 主机详情
     * 
     * @return <br>
     */
    @RequestMapping("/serverInfo")
    public String getServerInfo(ModelMap model, String localId, String nodeIp) {
        model.put("localId", localId);
        model.put("nodeIp", nodeIp);
        return "/local/server_info";
    }

    @RequestMapping(value = "getNodesList", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getNodesList(HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add("hadoop");
        params.put("target", XMLRead.AIM);
        params.put("option", "getallnodes");
        params.put("services", "all");
        // params.put("object", "parameters");

        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "addNewNode", produces = "application/json;charset=UTF-8")
    public @ResponseBody String addNewNode(HttpServletResponse response, String nodeIp, Form form) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", form.getIp());
        params.put("option", "addnode");
        params.put("services", "all");
        params.put("object", "all");

        DbInfo dbInfo = XMLRead.xmlDbInfo();
        Map<String, Object> paramsObj = new HashMap<String, Object>();
        paramsObj.put("type", dbInfo.getType());
        paramsObj.put("ip", dbInfo.getIp());
        paramsObj.put("port", Integer.parseInt(dbInfo.getPort()));
        paramsObj.put("dbname", dbInfo.getDbname());
        paramsObj.put("user", dbInfo.getUser());
        paramsObj.put("passwd", dbInfo.getPassword());

        Map<String, Object> paramObject2 = new HashMap<String, Object>();
        paramObject2.put("dbinfo", paramsObj);
        params.put("addnode", paramObject2);

        // paramObject2.put("desc", form.getDesc());
        paramObject2.put("hostname", form.getOs());

        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "getCollectInfo", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getCollectInfo(HttpServletResponse response, String nodeIp) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add(nodeIp);
        params.put("target", nodeIp);
        params.put("option", "getnodeinfo");
        params.put("nodes", list);
        params.put("object", "info");
        // params.put("object", "parameters");

        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "delFreeNode", produces = "application/json;charset=UTF-8")
    public @ResponseBody String delFreeNode(HttpServletResponse response, String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", ip);
        params.put("option", "delnode");
        params.put("nodes", "all");
        params.put("object", "all");
        // params.put("object", "parameters");

        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

}
